/*
 * Reflexis is Copyright (C) 2007, by it-raum.ch
 * martin.imobersteg@it-raum.ch
 *
 * It is free software; you can redistribute it and/or modify it under the terms of either:
 * a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or
 * b) the "Artistic License".
 */

#include "ProbeWriter.h"

#include <QtGui>
#include <QtSql>
#include <QtDebug>

ProbeWriter::ProbeWriter(QObject *parent) : QObject(parent)
{
	m_db = QSqlDatabase::addDatabase("QPSQL");
}

ProbeWriter::~ProbeWriter()
{
	m_db.close();
}

bool ProbeWriter::connect()
{
	m_db.close();

	QSettings settings;

	qDebug() << "connecting to db: " << settings.value("username").toString() << ":"
		<< settings.value("database").toString() << "@"
		<< settings.value("hostname").toString();

	m_db.setHostName(settings.value("hostname").toString());
	m_db.setDatabaseName(settings.value("database").toString());
	m_db.setUserName(settings.value("username").toString());
	m_db.setPassword(settings.value("password").toString());

	if (!m_db.open())
	{
		QMessageBox::warning(0, tr("Reflexis"), tr("Could not connect to db.\n"));
		qDebug("could not connect to db");
		return false;
	}

	qDebug("connected");
	return true;
}

QString ProbeWriter::getClientName(QString id)
{
	if (m_db.isOpen())
	{
		// Get the patient id
		QString queryString = "SELECT bezeichnung1, bezeichnung2 FROM kontakt WHERE patientnr = " + id;
		QSqlQuery patientQuery(queryString);
		patientQuery.next();
		QString patientName = patientQuery.value(0).toString() + " " + patientQuery.value(1).toString() + " (" + id + ")";
		
		return patientName;
	}
	
	return "NOK";
}

void ProbeWriter::writeProbe(Probe probe)
{
	// select max(id) from laborwerte where id like '0000000000000%';
	
	qDebug() << "write probe" << probe.getResult().size();
	
	if (probe.getResult().size() != 24)
	{
		qDebug() << "result to short";
		//return;
	}
	
	QString paramName;
	QString value;
	
	if (probe.getResult().size() > 19)
	{
		if (probe.getResult().at(20) == 'C')
		{
			// Enzym
			paramName = probe.getResult().mid(0,4).trimmed().toUpper();
			value = probe.getResult().mid(4,6).trimmed();
		}
		else
		{
			// Substrat
			paramName = probe.getResult().mid(0,4).trimmed().toUpper();
			value = probe.getResult().mid(5,6).trimmed();
		}
	}
	else
	{
		// Substrat
		paramName = probe.getResult().mid(0,4).trimmed().toUpper();
		value = probe.getResult().mid(5,6).trimmed();
	}
	
	qDebug() << "parameter" << paramName;
	qDebug() << "value" << value;
	
	QString datum = "20" + probe.getDate().mid(6,2) + probe.getDate().mid(3,2) + probe.getDate().mid(0,2);
	qDebug() << "datum" << datum;
	
	if (m_db.isOpen())
	{
		// Get the parameter id
		QString queryString = "SELECT id FROM laboritems WHERE kuerzel like '" + paramName + "'";
		QSqlQuery laboritemQuery(queryString);
		laboritemQuery.next();
		QString laboritemId = laboritemQuery.value(0).toString();
		
		qDebug() << "laboritemId: " << laboritemId;
		
		// Get the patient id
		queryString = "SELECT id FROM kontakt WHERE patientnr = " + probe.getIdent();
		QSqlQuery patientQuery(queryString);
		patientQuery.next();
		QString patientId = patientQuery.value(0).toString();
		
		qDebug() << "patientId: " << patientId;
		
		// Get us an id
		QSqlQuery idQuery("SELECT max(id) FROM laborwerte WHERE id like '0000000000000%'");
		idQuery.next();
		QString lastId = idQuery.value(0).toString();
		qDebug() << "last id: " << lastId;

		int newId = lastId.toInt() + 1;

		if (newId == 1)
			newId = 1000000;

		qDebug() << "new id: " << newId;
		
		qDebug() << "INSERT INTO laborwerte (id, patientid, datum, itemid, resultat) VALUES " << QString("0000000000000%1").arg(newId) <<
		patientId << datum << laboritemId << value << "0";

		
		QSqlQuery query;
		query.prepare("INSERT INTO laborwerte (id, patientid, datum, itemid, resultat) "
					   	"VALUES (:id, :patientid, :datum, :itemid, :resultat)");
		query.bindValue(":id", QString("0000000000000%1").arg(newId) );
		query.bindValue(":patientid", patientId);
	    query.bindValue(":datum", datum);
	    query.bindValue(":itemid", laboritemId);
	    query.bindValue(":resultat", value);
	    query.exec();
		
	}
}

